# Copyright (c) 2022 Presto Labs Pte. Ltd.
# Author: daniel

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


trade_symbol = ['ETH-USDC', 'ETH-USDT', 'ETH-DAI', 'BTC-USDC']


def gen_customized_native_symbol():
  symbols = dict()
  symbols['ETH-USDC'] = {
    "base_symbol": "ETH",
    "quote_symbol": "USDC",
    "price_ticksize": 0.01,
    "qty_ticksize": 0.01,
    "min_qty": 0.01,
    "min_amount": 10.0,
  }
  symbols['ETH-USDT'] = {
    "base_symbol": "ETH",
    "quote_symbol": "USDT",
    "price_ticksize": 0.01,
    "qty_ticksize": 0.01,
    "min_qty": 0.01,
    "min_amount": 10.0,
  }
  symbols['ETH-DAI'] = {
    "base_symbol": "ETH",
    "quote_symbol": "DAI",
    "price_ticksize": 0.01,
    "qty_ticksize": 0.01,
    "min_qty": 0.01,
    "min_amount": 10.0,
  }
  symbols['BTC-USDC'] = {
    "base_symbol": "BTC",
    "quote_symbol": "USDC",
    "price_ticksize": 0.01,
    "qty_ticksize": 0.001,
    "min_qty": 0.001,
    "min_amount": 10.0,
  }
  return symbols


def gen_var_price_ticks(info):
  price_list = [1e9, 0]
  tick_list = []
  tick_list.append(info['price_ticksize'])

  range_list = list(zip(price_list[:-1], price_list[1:]))
  result = []
  for r, tick in zip(range_list, tick_list):
    assert r[0] > r[1]
    info = {
        'from': r[1],
        'to': r[0],
        'value': tick,
    }
    result.append([info['from'], info['to'], info['value']])
  return result


def gen_var_qty_ticks(info):
  qty_ticksize = info['qty_ticksize']
  return qty_ticksize


def generate():
  mea = 'Spot.Uniswap.v3'
  me = 'Spot.Uniswap'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = gen_customized_native_symbol()

  pis = []
  for info in data.values():
    native_base = info['base_symbol']
    native_quote = info['quote_symbol']
    native_symbol = f"%s-%s" % (native_base, native_quote)
    if native_symbol not in trade_symbol:
      continue
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = f"%s-%s" % (base, quote)

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        is_inverse=False,
        price_ticksize=gen_var_price_ticks(info),
        qty_ticksize=gen_var_qty_ticks(info),
        min_qty=float(info['min_qty']),
        min_amount=float(info['min_amount']),
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  symbol_list = [info['native_symbol'] for info in data]
  quote_currency_list = list({info['native_quote'] for info in data})
  base_currency_list = list({info['native_base'] for info in data})
  currency_list = base_currency_list + quote_currency_list
  res = {
    'coin_list': list(set(base_currency_list)),
    'symbol_list': symbol_list,
    'currency_list': currency_list,
    'quote_currency_list': quote_currency_list,
  }
  return res


if __name__ == '__main__':
  import json
  config = generate()
  print(json.dumps(config, indent=2))
